package by.grsu.coctails.db.task;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import by.grsu.coctails.db.DBManager;

public abstract class CommonGetAsyncTask<B> extends
		AsyncTask<Void, Void, List<B>> {

	private DBManager dbManager;

	public CommonGetAsyncTask(Context context) {
		dbManager = DBManager.get(context);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		showLoading();
	}

	@Override
	protected void onPostExecute(List<B> result) {
		super.onPostExecute(result);
		hideLoading();
		onComplete(result);
	}

	@Override
	protected List<B> doInBackground(Void... arg0) {
		List<B> result = load();
		return onPrepareResult(result);
	}

	protected abstract List<B> load();

	public abstract void onComplete(List<B> result);

	public List<B> onPrepareResult(List<B> result) {
		return result;
	}

	protected void showLoading() {

	}

	protected void hideLoading() {

	}

	public DBManager getDbManager() {
		return dbManager;
	}

	public void start() {
		if (Build.VERSION.SDK_INT < 11) {
			execute();
		} else {
			executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}

}
